package javax;

/**
 * Created by xupeng20 on 2019/1/12.
 */
public class Xp_0116 {
    /**
     * 用数组结构实现大小固定的栈
     */
    private Integer[] arr;
    private Integer index;
    public Xp_0116(int initSize){
        if (initSize<0){
            throw new IllegalArgumentException("the init size is less than 0");
        }
        arr = new Integer[initSize];
        index = 0;
    }
    //查看
    public Integer peek(){
        if (index == 0){
            return null;
        }
        return arr[index-1];
    }
    //加数
    public void push(int obj){
        if (index ==arr.length){
            throw  new ArrayIndexOutOfBoundsException("full");
        }
        arr[index ++] =obj;
    }
    //减少
    public Integer pop(){
        if (index ==0){
            throw new ArrayIndexOutOfBoundsException("empty");
        }
        return arr[--index];
    }
}
